/*
 *    Copyright (c) 2018-2025, lengleng All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: lengleng (wangiegie@gmail.com)
 */
package com.pig4cloud.ila.basic.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import com.pig4cloud.ila.basic.api.dto.MemberDTO;
import com.pig4cloud.ila.basic.api.dto.MemberInfo;
import com.pig4cloud.ila.basic.api.entity.BasicMember;
import com.pig4cloud.ila.basic.mapper.BasicMemberMapper;
import com.pig4cloud.ila.basic.service.BasicMemberService;
import com.pig4cloud.pig.common.core.constant.QueryConstants;
import com.pig4cloud.pig.common.core.constant.enums.ResultEnum;
import org.springframework.beans.BeanUtils;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 会员表
 *
 * @author KonBAI
 * @date 2020-10-04 21:13:57
 */
@Service
public class BasicMemberServiceImpl extends ServiceImpl<BasicMemberMapper, BasicMember> implements BasicMemberService {

    private static final PasswordEncoder ENCODER = new BCryptPasswordEncoder();

    /**
     * 通过查会员的全部信息
     * @param member 会员
     * @return
     */
    @Override
    public MemberInfo getMemberInfo(BasicMember member) {
        MemberInfo memberInfo = new MemberInfo();
        memberInfo.setBasicMember(member);
        return memberInfo;
    }

    /**
     * 保存会员信息
     * @param memberDTO
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean saveMember(MemberDTO memberDTO) {
        BasicMember member = new BasicMember();
        BeanUtils.copyProperties(memberDTO, member);
        // 对密码进行加密
        member.setPassword(ENCODER.encode(memberDTO.getPassword()));
        return SqlHelper.retBool(baseMapper.insert(member));
    }

    /**
     * 注册会员
     * @param member
     * @return
     */
    @Override
    public ResultEnum register(BasicMember member) {
        int oleMemberNum = count(Wrappers.lambdaQuery(new BasicMember())
                .eq(BasicMember::getUsername, member.getUsername())
                .last(QueryConstants.SELECT_LiMIT_ONE));
        if (SqlHelper.retBool(oleMemberNum)) {
            ResultEnum.ERROR.append("该用户名已被使用！");
        }
        // 对密码进行加密
        member.setPassword(ENCODER.encode(member.getPassword()));
        if (!save(member)) {
            return ResultEnum.ERROR;
        }
        return ResultEnum.SUCCESS;
    }

}
